package com.wiegand.content.contents.textcontent {
	import flash.filters.DropShadowFilter;
	import com.caurina.transitions.Tweener;
	import com.caurina.transitions.properties.FilterShortcuts;
	import com.five3D.display.Shape3D;
	import com.five3D.display.Sprite3D;
	import com.five3D.typography.HelveticaBold;
	import com.pixelwelders.textcloud.MotifCollection3D;

	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;

	/**
	 * @author kwiegand
 	**/
 	
public class TextBlock3D extends Sprite3D {

		private var _xml : XMLList;
		private var _hit : Shape3D;
		private var _lines : Sprite3D;

		public function TextBlock3D(xml:XMLList){
			_xml = xml;
			init();
		}
		
		private function init() : void {	
			FilterShortcuts.init();
			
			_lines = new Sprite3D();
			addChild(_lines);
			
			var fooy : int = 0;
			for (var i : int = 0; i < _xml.children().length(); i++) {
				var line : MotifCollection3D = new MotifCollection3D( _xml.child(i), HelveticaBold, int(_xml.child(i).@size), uint(String(_xml.child(i).@color).replace("#","0x")) );
				line.y = fooy;
				line.mouseEnabled=false;
				line.x = int(_xml.child(i).@xSpace);
				line.z = int(_xml.child(i).@z);
				fooy += int(_xml.child(i).@height);
				_lines.addChild(line);
			}
			
			var rect : Array = String(_xml.@hitRect).split(",");
			
			_hit = new Shape3D();
			addChild(_hit);			
			_hit.graphics3D.beginFill(0xffffff, Number(_xml.@hitAlpha));
			_hit.graphics3D.drawRect(int(rect[0]), int(rect[1]), int(rect[2]), int(rect[3]));
			_hit.graphics3D.endFill();
			
			if(_xml.@url != null && _xml.@url != ""){
				buttonMode = true;
				addEventListener(MouseEvent.CLICK, click);
				addEventListener(MouseEvent.MOUSE_OVER, over);
				addEventListener(MouseEvent.MOUSE_OVER, out);
			}
			
			filters = [new DropShadowFilter(3,45,0xffffff,1,0,0,1,1)];
		}
		
		private function click(e:MouseEvent) : void {
			var request : URLRequest = new URLRequest( String(_xml.@url));
			navigateToURL(request,"_blank");		
		}

		private function over(e:MouseEvent) : void {
			Tweener.removeTweens(this);
			filters = [new GlowFilter( 0x000000, 1, 10, 10, 1, 3), new DropShadowFilter(3,45,0xffffff,1,0,0,1,1)];
		}

		private function out(e:MouseEvent) : void {
			Tweener.addTween(this,{_Glow_alpha:0, _Glow_blurY:0, delay:0.5, time:1, transition:"linear"});
		}

		public function explode(seconds:Number = 4, delay:Number = 0, ease:Function = null, distance:Number = 600) : void {
			for (var i : int = 0;i < _lines.numChildren; i++) {
				var line : MotifCollection3D = MotifCollection3D( _lines.getChildAt(i) );
				line.explode( seconds, delay, ease, distance );
			}			
		}

		public function assemble(seconds:Number = 4, delay:Number = 0, ease:Function = null) : void {
			for (var i : int = 0;i < _lines.numChildren; i++) {
				var line : MotifCollection3D = MotifCollection3D( _lines.getChildAt(i) );
				line.assemble(seconds, delay, ease);
			}			
		}

		public function destroy() : void {
			removeChild(_hit);	
			removeChild(_lines);	
			removeEventListener(MouseEvent.CLICK, click);
			removeEventListener(MouseEvent.MOUSE_OVER, over);
			removeEventListener(MouseEvent.MOUSE_OVER, out);
			Tweener.removeTweens(this);
			for (var i : int = 0;i < _lines.numChildren; i++) {
				var line : MotifCollection3D = MotifCollection3D( _lines.getChildAt(i) );
				line.destroy();
				line = null;
			}			
		}
			
	}
}
